<template>
  <div v-if="filePage">
    <el-form :inline="true" size="small">
      <el-form-item label="文件名称">
        <el-input
          placeholder="请输入文件名称"
          :prefix-icon="Search"
          v-model="fileName"
          class="input-with-select"
          width="120px"
        ></el-input>
      </el-form-item>
      <el-form-item label="文件类型">
        <el-select v-model="fileTypeId" placeholder="请选择文件类型">
          <el-option label="所有" value=""></el-option>
          <el-option
            v-for="fileType in fileTypeList"
            :key="fileType.id"
            :label="fileType.type"
            :value="fileType.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="find" :icon="Search">查询</el-button>
        <el-button type="success" :icon="Plus" @click="addPage()" size="small"
          >添加</el-button
        >
      </el-form-item>
    </el-form>
    <el-table :data="fileData" stripe style="width: 100%" border size="mini">
      <el-table-column prop="id" label="num" width="100"></el-table-column>
      <el-table-column
        prop="fileName"
        label="文件名称"
        width="180"
      ></el-table-column>
      <el-table-column prop="filePath" label="文件路径"></el-table-column>
      <el-table-column
        prop="fileTypeId"
        label="文件类型id"
        width="80"
      ></el-table-column>
      <el-table-column
        prop="passageId"
        label="图片关联文章id"
      ></el-table-column>
      <el-table-column
        prop="activityId"
        label="图片关联活动id"
      ></el-table-column>
      <el-table-column fixed="right" label="操作" width="280">
        <template #default="scope">
          <el-button
            type="primary"
            :icon="Edit"
            @click="editPage(scope.row)"
            size="mini"
            >编辑</el-button
          >
          <el-button type="success" :icon="Plus" @click="addPage()" size="mini"
            >添加</el-button
          >
          <el-button
            type="danger"
            :icon="Delete"
            @click="deleteFile(scope.row)"
            size="mini"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      v-model:current-page="currentPage"
      :total="filePage.total"
      @current-change="refreshFilePage"
      :page-size="filePage.pageSize"
    ></el-pagination>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { useRouter } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
import { Search, Edit, Plus, Delete } from "@element-plus/icons-vue";
import axios from "axios";

const OK = 200;
const router = useRouter();

const filePage = ref(null);
const fileData = ref([]);
const fileTypeId = ref(3);
const fileName = ref(null);
const fileTypeList = ref([]);
const currentPage = ref(1);

const getFilePage = async (pageNum, pageSize) => {
  try {
    const res = await axios.get("/api/files", {
      params: {
        fileTypeId: fileTypeId.value,
        fileName: fileName.value,
        pageNum,
        pageSize,
      },
    });
    if (res.data.code === OK) {
      filePage.value = res.data.data;
      fileData.value = filePage.value.list;
    } else {
      ElMessage.error(res.data.data);
    }
  } catch (error) {
    console.error(error);
    ElMessage.error("获取文件列表失败");
  }
};

const getFileTypeList = async () => {
  try {
    const res = await axios.get("/api/fileTypes");
    if (res.data.code === OK) {
      fileTypeList.value = res.data.data;
    } else {
      ElMessage.error(res.data.data);
    }
  } catch (error) {
    console.error(error);
    ElMessage.error("获取文件类型列表失败");
  }
};

const editPage = (row) => {
  router.push({ name: "EditFile", query: { id: row.id } });
};

const addPage = () => {
  router.push({ name: "AddFile" });
};

const deleteDao = async (fileId) => {
  try {
    const res = await axios.delete("/api/files/" + fileId);
    if (res.data.code === OK) {
      ElMessage.success("删除成功");
      getFilePage(currentPage.value, 8);
    } else {
      ElMessage.error(res.data.data);
    }
  } catch (error) {
    console.error(error);
    ElMessage.error("删除文件失败");
  }
};

const deleteFile = (row) => {
  ElMessageBox.confirm("此操作将永久删除该文件, 是否继续?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    deleteDao(row.id);
  });
};

const find = () => {
  getFilePage(currentPage.value, 8);
};

const refreshFilePage = () => {
  getFilePage(currentPage.value, 8);
};

onMounted(() => {
  getFilePage(currentPage.value, 8);
  getFileTypeList();
});
</script>

<style scoped>
</style>
